package com.jservice.web.sysRole;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jservice.framework.dto.Pageable;
import com.jservice.framework.dto.Pager;
import com.jservice.framework.idWorker.Sequence;
import com.jservice.framework.spring.SpringContextUtil;
import com.jservice.model.SysRole;
import com.jservice.model.SysRolePermission;
import com.jservice.model.SysUserRole;
import com.jservice.web.base.JBaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * SysRoleQuery
 *
 * @author cwvian
 * @version 1.0
 * @date 2017/5/3
 */
@Service("sysRoleQuery")
public class SysRoleService extends JBaseService {


	public static final SysRole DAO = new SysRole();

	public static SysRoleService me() {
		return SpringContextUtil.getBean("sysRoleQuery");
	}


	@Resource
	private Sequence idWorker;

	@Transactional(readOnly = true)
	public Pager findPage(Pageable pageable) {
		return findPage(pageable,DAO,SysRole.TABLE_NAME);
	}

	@Transactional(readOnly = true)
	public Record findById(String roleId) {
		String sql = "select sr.* ,p.permissions from "+SysRole.TABLE_NAME+" sr " +
				"left join "+ SysRolePermission.TABLE_NAME+" p on p.role_id = sr.id " +
				"where sr.id = ? ";

		Record role = Db.findFirst(sql,roleId);
		Record userRole = Db.findFirst( "select * from "+ SysUserRole.TABLE_NAME+" where role_id = ? ",roleId);

		if(userRole != null){
			role.set("is_delete",Boolean.FALSE);
		}else{
			role.set("is_delete",Boolean.TRUE);
		}
		return role;
	}

    @Transactional(readOnly = false)
	public boolean savePermission(SysRole role, String[] permissions) {
		if(role == null){
			return false;
		}
		role.setId(idWorker.nextIdStr());
		role.setCreateDate(new Date());
		role.setModifyDate(role.getCreateDate());
		role.save();

		SysRolePermission rolePermission = new SysRolePermission();
		rolePermission.setId(role.getId());
		rolePermission.setRoleId(role.getId());
		rolePermission.setPermissions(StringUtils.join(permissions, ","));
		return rolePermission.save();

	}

	@Transactional(readOnly = false)
	public boolean updatePermission(SysRole role, String[] permissions) {
		if(role == null){
			return false;
		}
		role.setModifyDate(new Date());
		role.removeNullValueAttrs();
		role.update();
		SysRolePermission rolePermission = new SysRolePermission();
        rolePermission.setId(role.getId());
		rolePermission.setRoleId(role.getId());
		rolePermission.setPermissions(StringUtils.join(permissions,","));

		return rolePermission.update();
	}
	@Transactional(readOnly = true)
	public Object findAll() {
		return DAO.find("select * from "+SysRole.TABLE_NAME);
	}

	@Transactional(readOnly = true)
	public List<String> findPermissionsByUserId(String userId){
		String sql = "SELECT rp.permissions FROM sys_user_role ur, sys_role_permission rp WHERE ur.user_id = ? and ur.role_id = rp.role_id";
		return Db.query(sql,userId);
	}
}
